package ar.com.ctrln.view.ui.comportamientoVentana;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zul.Toolbar;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

public abstract class AbstractMinimizarVentana extends
		SelectorComposer<Component> implements ComportamientoVentana {

	private static final long serialVersionUID = 1413575326081320430L;

	@Listen("onMinimize = window")
	public void onMinimize(Event evt) {
		Window window = (Window) evt.getTarget();
		if (window.isMinimized()) {
			
			Toolbar toolbar = (Toolbar) Executions.getCurrent().getDesktop().getFirstPage().getFellow(
					"barraEstado");
			Toolbarbutton minimized = new Toolbarbutton();
			minimized.setLabel(window.getTitle());
			minimized.setImage("/resources/icons/restaurar2.png");
			minimized.setId(Integer.toString(toolbar.getChildren().size() - 1));
			minimized.setHeight("35px");
			minimized.setStyle("font-size:12px;font-color:grey;");
			minimized.setParent(toolbar);
			minimized.setAttribute("ventana", window);
			addEvent(minimized);
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void addEvent(final Toolbarbutton button) {
		final Toolbar toolbar = (Toolbar) Executions.getCurrent().getDesktop().getFirstPage().getFellow(
				"barraEstado");
		button.addEventListener("onClick", new EventListener() {

			@Override
			public void onEvent(Event arg0) throws Exception {

				Window window = (Window) button.getAttribute("ventana");
				window.setVisible(true);

				toolbar.removeChild(button);

			}
		});
	}

}
